﻿namespace OboutInc
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Globalization;

    internal class CallbackCollectionConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType is ICollection) || base.CanConvertFrom(context, sourceType));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (((destinationType.GetInterface("IList", false) == null) && (destinationType.GetInterface("ICollection", false) == null)) && ((destinationType.GetInterface("IDictionary", false) == null) && !destinationType.IsArray))
            {
                return base.CanConvertTo(context, destinationType);
            }
            return true;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is IDictionary)
            {
                CallbackCollection callbacks = new CallbackCollection(true);
                IDictionaryEnumerator enumerator = ((IDictionary) value).GetEnumerator();
                enumerator.Reset();
                while (enumerator.MoveNext())
                {
                    callbacks.Add(enumerator.Key, enumerator.Value);
                }
                return callbacks;
            }
            if (!(value is ICollection))
            {
                return base.ConvertFrom(context, culture, value);
            }
            CallbackCollection callbacks2 = new CallbackCollection(false);
            IEnumerator enumerator2 = ((ICollection) value).GetEnumerator();
            enumerator2.Reset();
            while (enumerator2.MoveNext())
            {
                callbacks2.Add(null, enumerator2.Current);
            }
            return callbacks2;
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (!destinationType.IsArray)
            {
                if (destinationType.GetInterface("IDictionary", false) != null)
                {
                    object obj2;
                    if (destinationType.GetConstructor(Type.EmptyTypes) != null)
                    {
                        obj2 = destinationType.GetConstructor(Type.EmptyTypes).Invoke(null);
                    }
                    else
                    {
                        obj2 = new Hashtable();
                    }
                    IDictionaryEnumerator enumerator2 = (IDictionaryEnumerator) ((CallbackCollection) value).GetEnumerator();
                    enumerator2.Reset();
                    while (enumerator2.MoveNext())
                    {
                        ((IDictionary) obj2).Add(enumerator2.Key, enumerator2.Value);
                    }
                    return obj2;
                }
                if (destinationType.GetInterface("IList", false) != null)
                {
                    object obj3;
                    if (destinationType.GetConstructor(Type.EmptyTypes) != null)
                    {
                        obj3 = destinationType.GetConstructor(Type.EmptyTypes).Invoke(null);
                    }
                    else
                    {
                        obj3 = new ArrayList();
                    }
                    IEnumerator enumerator3 = ((CallbackCollection) value).GetEnumerator();
                    enumerator3.Reset();
                    while (enumerator3.MoveNext())
                    {
                        ((IList) obj3).Add(enumerator3.Current);
                    }
                    return obj3;
                }
                if (destinationType.GetInterface("ICollection", false) == null)
                {
                    return base.ConvertTo(context, culture, value, destinationType);
                }
                ICollection is2 = new ArrayList();
                IEnumerator enumerator4 = ((CallbackCollection) value).GetEnumerator();
                enumerator4.Reset();
                while (enumerator4.MoveNext())
                {
                    ((ArrayList) is2).Add(enumerator4.Current);
                }
                return is2;
            }
            Array array = Array.CreateInstance(destinationType.GetElementType(), ((CallbackCollection) value).Count);
            IEnumerator enumerator = ((CallbackCollection) value).GetEnumerator();
            enumerator.Reset();
            int index = 0;
            while (enumerator.MoveNext())
            {
                try
                {
                    array.SetValue((enumerator.Current is DictionaryEntry) ? ((DictionaryEntry) enumerator.Current).Value : enumerator.Current, index);
                    index++;
                    continue;
                }
                catch (Exception exception)
                {
                    throw new InvalidCastException("Cannot convert item at index " + ((enumerator.Current is DictionaryEntry) ? ("'" + ((DictionaryEntry) enumerator.Current).Key + "'") : index.ToString()) + " to " + destinationType.GetElementType().Name + ".", exception);
                }
            }
            return array;
        }
    }
}

